Ext.onReady(function() {
	Ext.define('LoginModel', {
        extend: 'Ext.data.Model',
        fields: [
            {name: 'ID', type:'string'},
            {name: 'USRID', type:'string'},
            {name: 'USERNAME', type:'string'},
            {name: 'LOGINTIME', type:'string'},
            {name: 'LOGOUTTIME', type:'string'},
            {name: 'REMOTHOST', type:'string'},
            {name: 'SESSIONID', type:'string'},
            {name: 'STATUS', type:'string'}
        ]
    });
	
	var loginStore = Ext.create('Ext.data.Store',{
		model : 'LoginModel',
		pageSize : 50
	});
	
	var grouping = Ext.create('Ext.grid.feature.GroupingSummary',{
		groupHeaderTpl: '{name}',
        hideGroupedHeader: true,
        enableGroupingMenu: false
    });
	
	var user = Ext.create('Ext.form.field.Text',{
        fieldLabel: str_onlineuser_user,
        labelWidth: 40,
        width : 150
	});
	
	var startDate = Ext.create('Ext.form.field.Date',{
        fieldLabel: str_onlineuser_logindate,
        width : 200,
        labelWidth: 80
	});
	var to = Ext.create('Ext.form.Label',{
		text:'~'
	});
	var endDate = Ext.create('Ext.form.field.Date',{
        width : 120
	});
	
	var states = Ext.create('Ext.data.Store', {
	    fields: ['val', 'name'],
	    data : [
	        {"val":"", "name":str_onlineuser_all},
	        {"val":"1", "name":str_onlineuser_online},
	        {"val":"0", "name":str_onlineuser_deline}
	    ]
	});
	
	var online = Ext.create('Ext.form.field.ComboBox',{
		fieldLabel: str_onlineuser_isonline,
        queryMode: 'local',
        displayField: 'name',
        valueField: 'val',
        store: states,
        labelWidth: 80,
        width: 180,
        value: '1'
	});

	var loginGrid = Ext.create('Ext.grid.Panel', {
		split: true,
		loadMask : true,
		multiSelect : true,
		store:loginStore,
		region: 'center',
		border: false,
		features: [
		   grouping
		],
		columns: [
          {
              xtype: 'rownumberer',
              width: 30,
              hideable: false
          },
          {
        	  xtype: 'gridcolumn',
        	  text: str_onlineuser_uid,
              width: 120,
              hideable: false,
              dataIndex: 'USRID'
          },
          {
        	  xtype: 'gridcolumn',
        	  text: str_onlineuser_uname,
        	  hideable: false,
              width: 150,
              dataIndex: 'USERNAME'
          },
          {
              xtype: 'gridcolumn',
              dataIndex: 'LOGINTIME',
              hideable: false,
              text: str_onlineuser_logintime,
              width : 150
          },
          {
              xtype: 'gridcolumn',
              dataIndex: 'LOGOUTTIME',
              hideable: false,
              text: str_onlineuser_exittime,
              width : 150
          },
          {
              xtype: 'gridcolumn',
              hideable: false,
              dataIndex: 'REMOTHOST',
              text: str_onlineuser_host,
              width : 150
          },
          {
              xtype: 'gridcolumn',
              hideable: false,
              dataIndex: 'STATUS',
              text: str_onlineuser_isonline,
              width : 80
          },
          {
              xtype: 'gridcolumn',
              hideable: false,
              dataIndex: 'SESSIONID',
              text: str_onlineuser_sid,
              flex: 1,
              width : 200
          },{
              xtype: 'actioncolumn',
              draggable: false,
              sortable: false,
              hideable: false,
              width: 30,
              items: [
                  {
                	  icon:getPath('/common/images/delete.png'),
                	  handler : function(grid, rowIndex, colIndex){
                		  var id = loginStore.getAt(rowIndex).get('ID');
                		  Ext.Ajax.request({
							  url : getPath('/aa/po/userManager.do'),
							  params : {
								  md : 'deletehistory',
								  ID : id
							  },
							  actionMethods: 'POST',
							  success : function(response) {  
					            	var ret = Ext.JSON.decode(response.responseText);
					            	if(ret.success){
					            		var sm = grid.getSelectionModel();
					            		sm.select(rowIndex);
					            		loginStore.remove(sm.getSelection());
					            	}
					            	else {
					            		Ext.MessageBox.alert(str_common_error,ret.msg);
					            	}
					          }
						  });
                	  }
                  }
              ]
          }
        ],
        dockedItems: [
		  {
		      xtype: 'toolbar',
		      dock: 'top',
		      items: [user,'-',startDate,to,endDate,'-',online,'-',
                  {
                	  xtype: 'button',
                	  text: str_common_query,
                	  listeners: {
                          click: {
                        	  fn:query
                          }
                      }
                  }
		      ]
		   },
		],
		bbar : Ext.create('Ext.PagingToolbar', {
			store : loginStore,
			displayInfo : true
		})
	});
	
	function query(){
		loginStore.setProxy({
			type : 'ajax',
			actionMethods: 'POST',
			url : getPath("/aa/po/userManager.do"),
			extraParams : {
            	"md":"readLoginUser",
            	'ID': user.getRawValue(),
        		'STARTDATE': startDate.getRawValue(), 
        		'ENDDATE': endDate.getRawValue(),
        		'STATUS': online.value
            },
			reader : {
				type : 'json',
				root : 'user',
				totalProperty : 'totalCount'
			}
		});
		loginStore.load();
	}
	
	var view = Ext.create('Ext.container.Viewport', {
		layout : 'border',
		renderTo : Ext.getBody(),
		items : [loginGrid]
	});
	query();
});